package com.example.for_screen_push;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;

public class MainActivity extends AppCompatActivity {
    SocketLive socketLive;
    MediaProjectionManager mediaProjectionManager;
    MediaService service;
    public boolean checkPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            },1);
        }
        return  false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
        startActivityForResult(captureIntent,1);
        checkPermission();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || requestCode != 1) return;
        startService(new Intent(this,MediaService.class));
//        MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode,data);
        Intent mIntent=new Intent(MainActivity.this,MediaService.class) ;
//        Intent service = new Intent(this, ScreenRecorder.class);
        mIntent.putExtra("code", resultCode);
        mIntent.putExtra("data", data);
        startForegroundService(mIntent);
        bindService(mIntent, ServiceConnection, Context.BIND_AUTO_CREATE);



    }


    private MediaService.Binder binder;
    ServiceConnection ServiceConnection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            binder = (MediaService.Binder) service;
            MediaService myService = binder.getMyService();
            myService.setCallback(new MediaService.Callback() {


                @Override
                public void onDataChange(MediaProjection mMediaProjection) {
                    if (mMediaProjection == null)return;
                    socketLive = new SocketLive();
                    socketLive.start(mMediaProjection);
                }
            });

        }
        // This is the old onStart method that will be called on the pre-2.0// platform. On 2.0 or later we override onStartCommand() so this// method will not be called.// 2.0 API level之后，实现onStart等同于重写onStartCommand并返回START_STICKY@Overridepublic void onStart(Intent intent, int startId) {handleCommand(intent);}// 2.0 API level之后，onStart()方法被onStartCommand()取代了@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {handleCommand(intent);// We want this service to continue running until it is explicitly// stopped, so return sticky.return START_STICKY;} 启动服务时依次执行onCreate，onStartCommand，onStart；如果在系统显示调用stopService和stopSelf之前终止服务，service再次重启，onStartCommand会被调用，重启服务时依次执行onStartCommand，onStart。无论何时，都会先调用onStartCommand()，在调用onStart()。onStartCommand返回值onStartComand使用时，返回的是一个(int)整形。这个整形可以有四个返回值：start_sticky、start_no_sticky、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY。它们的含义分别是：1):START_STICKY：如果service进程被kill掉，保留service的状态为开始状态，但不保留递送的intent对象。随后系统会尝试重新创建service，由于服务状态为开始状态，所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service，那么参数Intent将为null。2):START_NOT_STICKY：“非粘性的”。使用这个返回值时，如果在执行完onStartCommand后，服务被异常kill掉，系统不会自动重启该服务3):START_REDELIVER_INTENT：重传Intent。使用这个返回值时，如果在执行完onStartCommand后，服务被异常kill掉，系统会自动重启该服务，并将Intent的值传入。 4):START_STICKY_COMPATIBILITY：START_STICKY的兼容版本，但不保证服务被kill后一定能重启。onStartComand参数flags含义flags表示启动服务的方式：Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.START_FLAG_REDELIVERY：如果你实现onStartCommand()来安排异步工作或者在另一个线程中工作, 那么你可能需要使用START_FLAG_REDELIVERY来让系统重新发送一个intent。这样如果你的服务在处理它的时候被Kill掉, Intent不会丢失.START_FLAG_RETRY：表示服务之前被设为START_STICKY，则会被传入这个标记。

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

    };
}